package com.mixue.controller;


import com.mixue.model.Employee;
import com.mixue.restful.ResoutSet;
import com.mixue.service.EmployeeService;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployController {
    @Autowired
    private EmployeeService employeeService;
@RequestMapping("/getlist")
public List<Employee> getall(){
    return employeeService.list();
}
    //员工登录
    @PostMapping("/login")
    public ResoutSet<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        String password = employee.getPassword();
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);
        if (emp == null) {
            return ResoutSet.error("用户名不存在！");
        }
        if (!emp.getPassword().equals(password)) {
            return ResoutSet.error("密码不正确！");
        }
        request.getSession().setAttribute("employee", emp.getId());
        return ResoutSet.sussess(emp);
    }

    //    退出
    @PostMapping("/logout")
    public ResoutSet<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return ResoutSet.sussess("退出成功！");
    }

    //    新增员工
    @PostMapping
    public ResoutSet<String> add(@RequestBody Employee employee) {
        System.out.println(employee);
        employeeService.save(employee);
        return ResoutSet.sussess("添加成功！");
    }
    @DeleteMapping("/del/{id}")
    public ResoutSet<String> delete(@PathVariable String id){
        employeeService.removeById(id);
        return ResoutSet.sussess("删除成功！");
    }
    @GetMapping("/page")
    public ResoutSet<Page> page(int page,int pageSize,String name){
        Page pageInfo=new Page(page,pageSize);
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.like(StringUtils.isNotBlank(name),Employee::getName,name);
        queryWrapper.orderByDesc(Employee::getName);
        employeeService.page(pageInfo,queryWrapper);
        return ResoutSet.sussess(pageInfo);
    }
    @PutMapping
    public ResoutSet<String> update(@RequestBody Employee employee){
        System.out.println(employee);
        employeeService.updateById(employee);
        System.out.println("修改成功");
        return ResoutSet.sussess("修改成功！");
    }

    @GetMapping("/{id}")
    public ResoutSet<Employee> getById(@PathVariable Long id){
        Employee employee=employeeService.getById(id);
        if (employee!=null){
            return ResoutSet.sussess(employee);
        }
        return ResoutSet.error("查询失败");
    }
}
